using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Acme.BookStore.StockManagement.Dtos.KucunFenBu;
using Acme.BookStore.StockManagement.Services;
using AutoMapper;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Acme.BookStore.Repositories;

namespace Acme.BookStore.StockManagement
{
    /// <summary>
    /// 库存分布应用服务实现类
    /// </summary>
    public class KucunFenBuAppService : ApplicationService, IKucunFenBuAppService
    {
        private readonly IRepository<KucunFenBu, int> _kucunFenBuRepository;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="kucunFenBuRepository">库存分布仓储</param>
        /// <param name="mapper">对象映射</param>
        public KucunFenBuAppService(
            IRepository<KucunFenBu, int> kucunFenBuRepository,
            IMapper mapper)
        {
            _kucunFenBuRepository = kucunFenBuRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取库存分布分页列表
        /// </summary>
        /// <param name="input">查询条件</param>
        /// <returns>分页结果</returns>
        public async Task<PagedResultDto<KucunFenBuDto>> GetListAsync(KucunFenBuGetListInputDto input)
        {
            // 构建查询条件
            var query = _kucunFenBuRepository.GetQueryable();

            // 添加查询条件
            if (!string.IsNullOrEmpty(input.WarehouseName))
            {
                query = query.Where(x => x.WarehouseName == input.WarehouseName);
            }

            if (!string.IsNullOrEmpty(input.LocationName))
            {
                query = query.Where(x => x.LocationName.Contains(input.LocationName));
            }

            if (input.CurrentStockMin.HasValue)
            {
                query = query.Where(x => x.CurrentStock >= input.CurrentStockMin.Value);
            }

            if (input.CurrentStockMax.HasValue)
            {
                query = query.Where(x => x.CurrentStock <= input.CurrentStockMax.Value);
            }

            if (input.AvailableStockMin.HasValue)
            {
                query = query.Where(x => x.AvailableStock >= input.AvailableStockMin.Value);
            }

            if (input.AvailableStockMax.HasValue)
            {
                query = query.Where(x => x.AvailableStock <= input.AvailableStockMax.Value);
            }

            // 获取总数
            var totalCount = await AsyncExecuter.CountAsync(query);

            // 排序
            if (!string.IsNullOrEmpty(input.Sorting))
            {
                query = query.OrderBy(x => x.Id);
            }
            else
            {
                query = query.OrderByDescending(x => x.Id);
            }

            // 分页
            query = query.Skip(input.SkipCount).Take(input.MaxResultCount);

            // 获取数据
            var items = await AsyncExecuter.ToListAsync(query);

            // 转换为DTO
            var dtos = _mapper.Map<List<KucunFenBu>, List<KucunFenBuDto>>(items);

            // 返回分页结果
            return new PagedResultDto<KucunFenBuDto>(totalCount, dtos);
        }

        /// <summary>
        /// 获取库存分布详情
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns>库存分布</returns>
        public async Task<KucunFenBuDto> GetAsync(int id)
        {
            var entity = await _kucunFenBuRepository.GetAsync(id);
            return _mapper.Map<KucunFenBu, KucunFenBuDto>(entity);
        }

        /// <summary>
        /// 创建库存分布
        /// </summary>
        /// <param name="input">库存分布</param>
        /// <returns>创建后的库存分布</returns>
        public async Task<KucunFenBuDto> CreateAsync(KucunFenBuDto input)
        {
            var entity = _mapper.Map<KucunFenBuDto, KucunFenBu>(input);
            entity = await _kucunFenBuRepository.InsertAsync(entity);
            return _mapper.Map<KucunFenBu, KucunFenBuDto>(entity);
        }

        /// <summary>
        /// 更新库存分布
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="input">库存分布</param>
        /// <returns>更新后的库存分布</returns>
        public async Task<KucunFenBuDto> UpdateAsync(int id, KucunFenBuDto input)
        {
            var entity = await _kucunFenBuRepository.GetAsync(id);
            _mapper.Map(input, entity);
            entity = await _kucunFenBuRepository.UpdateAsync(entity);
            return _mapper.Map<KucunFenBu, KucunFenBuDto>(entity);
        }

        /// <summary>
        /// 删除库存分布
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            await _kucunFenBuRepository.DeleteAsync(id);
        }
    }
} 